<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Building a UI with AsyncQueue</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: Building a UI with AsyncQueue</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><style scoped>
    #init {
        margin-top: 1em;
    }

    #demo .yui3-module {
        position: relative;
        width: 28em;
    }
    #demo .yui3-module .yui3-hd,
    #demo .yui3-module .yui3-bd,
    #demo .yui3-module .yui3-ft {
        margin: 0;
        padding: 1ex 1em;
    }
    #demo .yui3-module .yui3-hd {
        background: #406ED9;
    }
    #demo .yui3-module .yui3-hd h4 {
        color: #fff;
        margin: 0;
    }
    #demo .yui3-module .yui3-bd {
        background: #ABCEFF;
        border-left: 1px solid #7A97BB;
        border-right: 1px solid #7A97BB;
        height: 5em;
        padding-top: 4.5em;
        position: relative;
        overflow: hidden;
        text-align: center;
    }
    #demo .yui3-module .yui3-ft {
        background: #fff;
        border: 1px solid #7A97BB;
        border-top-color: #ccc;
        padding-right: 25px;
    }
    #demo .yui3-module .yui3-status {
        margin: 0;
        padding: 0 25px 0 0;
        height: 1.3em;
    }
    #demo .yui3-module .yui3-nav {
        background: #fff;
        border-bottom: 1px solid #ccc;
        left: 0;
        padding: .5em;
        position: absolute;
        width: 27em;
    }
    #demo .yui3-module .yui3-nav ul,
    #demo .yui3-module .yui3-nav li {
        list-style: none;
        margin: 0;
        padding: 0;
    }
    #demo .yui3-module .yui3-nav a {
        color: #ffa928;
    }
    #demo .yui3-module .working {
        background: #fff url(http://l.yimg.com/a/i/nt/ic/ut/bsc/busyarr_1.gif) no-repeat 26em 50%;
    }
</style>


<div class="intro">
    <p>This example illustrates how to break up the initial rendering of an application UI into queued code chunks, yielding back to the browser regularly to draw portions of the UI as they become ready.</p>
    
<p><em>Note</em>: This method should be reserved for apps constructing complex DOM structures.  While the DOM structure contained in this example is not complex, some artificial delays are injected to simulate process-intensive operations that would normally cause such delays.</p>
</div>

<div class="example yui3-skin-sam">
<div id="demo">
    <p>The module will be inserted here.  <em>Click the button below</em>.</p>
</div>

<button id="init">Initialize Application</button>


<script type="text/javascript">
YUI().use("node", "transition","async-queue", function (Y) {

var MyApp = {
    NAME : 'Asynchronous Queue Demo',

    q : new Y.AsyncQueue(),

    nodes : {
        root    : null,
        status  : null,
        nav     : null,
        content : null,
        foot    : null
    },

    render : function (container) {
        if (MyApp.nodes.root) {
            MyApp.q.stop();
        }

        // artificial delays have been inserted to simulate _renderNav or
        // _renderContent being process intensive and taking a while to complete
        MyApp.q.add(
            // pass the container param to the callback using Y.bind
            Y.bind(MyApp._renderFramework, MyApp, container),
            {fn: function () {}, timeout: 700}, // artificial delay
            MyApp._renderNav,
            {fn: function () {}, timeout: 700}, // artificial delay
            MyApp._renderContent).run();
    },

    setStatus : function (message,working) {
        MyApp.nodes.status.setContent(message);

        MyApp.nodes.foot[working?'addClass':'removeClass']('working');
    },

    _renderFramework : function (container) {
        var root = MyApp.nodes.root = Y.one(container);

        root.setContent(
        '<div class="yui3-module">'+
            '<div class="yui3-hd">'+
                '<h4>'+MyApp.NAME+'</h4>'+
            '</div>'+
            '<div class="yui3-bd">'+
                '<div class="yui3-nav"></div>'+
                '<div class="yui3-content"></div>'+
            '</div>'+
            '<div class="yui3-ft">'+
                '<p class="yui3-status"></p>'+
            '</div>'+
        '</div>');

        MyApp.nodes.status  = root.one('p.yui3-status');
        MyApp.nodes.nav     = root.one('.yui3-nav');
        MyApp.nodes.content = root.one('.yui3-content');
        MyApp.nodes.foot    = root.one('.yui3-ft');

        MyApp.nodes.nav.setStyle('top','-30px');
        MyApp.nodes.content.setStyle('opacity',0);

        MyApp.setStatus('Loading...',true);
    },

    _renderNav : function () {
        var nav = MyApp.nodes.nav;
        nav.append(
            '<ul class="yui3-g">'+
                '<li class="yui3-u-1-4"><a href="#">Nav Lorem</a></li>'+
                '<li class="yui3-u-1-4"><a href="#">Nav Ipsum</a></li>'+
                '<li class="yui3-u-1-4"><a href="#">Nav Dolor</a></li>'+
                '<li class="yui3-u-1-4"><a href="#">Nav Sit</a></li>'+
            '</ul>');

        nav.transition({
            top: 0,
            duration: .3
        });

        // Stub some navigation behavior for the example
        nav.delegate('click', function (e) {
            e.preventDefault();
            MyApp.nodes.content
                .setContent('<p>Clicked on ' + this.get('text') + '</p>');
        }, 'a');
    },

    _renderContent : function () {
        MyApp.nodes.content
            .setContent('<p>[ App content here ]</p>')
            .transition({
                opacity: 1,
                duration: .8
            });

        MyApp.setStatus('App initialized',false);
    }
};

Y.one('#init').on('click',function (e) {
    e.preventDefault();
    this.set('text','Re-initialize Application');

    MyApp.render('#demo');
});

// expose the example structure
YUI.example = { MyApp : MyApp };

});
</script>

</div>

<h3>The Markup</h3>
<p>The markup will start with just a placeholder element for our application.</p>

<pre class="code prettyprint">&lt;div id=&quot;demo&quot;&gt;
    &lt;p&gt;The module will be inserted here.  &lt;em&gt;Click the button below&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;

&lt;button id=&quot;init&quot;&gt;Initialize Application&lt;&#x2F;button&gt;</pre>


<p>The markup will eventually evolve to the following as the script runs (indented for readability):</p>

<pre class="code prettyprint">&lt;div id=&quot;demo&quot;&gt;
    &lt;div class=&quot;yui3-module&quot;&gt;
        &lt;div class=&quot;yui3-hd&quot;&gt;
            &lt;h4&gt;AsyncQueue Demo&lt;&#x2F;h4&gt;
        &lt;&#x2F;div&gt;
        &lt;div class=&quot;yui3-bd&quot;&gt;
            &lt;div class=&quot;yui3-nav&quot;&gt;
                &lt;ul class=&quot;yui3-g&quot;&gt;
                    &lt;li class=&quot;yui3-u-1-4&quot;&gt;&lt;a href=&quot;#&quot;&gt;Nav Lorem&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
                    &lt;li class=&quot;yui3-u-1-4&quot;&gt;&lt;a href=&quot;#&quot;&gt;Nav Ipsum&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
                    &lt;li class=&quot;yui3-u-1-4&quot;&gt;&lt;a href=&quot;#&quot;&gt;Nav Dolor&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
                    &lt;li class=&quot;yui3-u-1-4&quot;&gt;&lt;a href=&quot;#&quot;&gt;Nav Sit&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
                &lt;&#x2F;ul&gt;
            &lt;&#x2F;div&gt;
            &lt;div class=&quot;yui3-content&quot;&gt;
                &lt;p&gt;[ App content here ]&lt;&#x2F;p&gt;
            &lt;&#x2F;div&gt;
        &lt;&#x2F;div&gt;
        &lt;div class=&quot;yui3-ft&quot;&gt;
            &lt;p class=&quot;yui3-status&quot;&gt;(status message here)&lt;&#x2F;p&gt;
        &lt;&#x2F;div&gt;
    &lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;

&lt;button id=&quot;init&quot;&gt;Re-initialize Application&lt;&#x2F;button&gt;</pre>


<h3>The CSS</h3>
<p>Some CSS is added to make it look like an application.</p>

<pre class="code prettyprint">&lt;style scoped&gt;
    #init {
        margin-top: 1em;
    }

    #demo .yui3-module {
        position: relative;
        width: 28em;
    }
    #demo .yui3-module .yui3-hd,
    #demo .yui3-module .yui3-bd,
    #demo .yui3-module .yui3-ft {
        margin: 0;
        padding: 1ex 1em;
    }
    #demo .yui3-module .yui3-hd {
        background: #406ED9;
    }
    #demo .yui3-module .yui3-hd h4 {
        color: #fff;
        margin: 0;
    }
    #demo .yui3-module .yui3-bd {
        background: #ABCEFF;
        border-left: 1px solid #7A97BB;
        border-right: 1px solid #7A97BB;
        height: 5em;
        padding-top: 4.5em;
        position: relative;
        overflow: hidden;
        text-align: center;
    }
    #demo .yui3-module .yui3-ft {
        background: #fff;
        border: 1px solid #7A97BB;
        border-top-color: #ccc;
        padding-right: 25px;
    }
    #demo .yui3-module .yui3-status {
        margin: 0;
        padding: 0 25px 0 0;
        height: 1.3em;
    }
    #demo .yui3-module .yui3-nav {
        background: #fff;
        border-bottom: 1px solid #ccc;
        left: 0;
        padding: .5em;
        position: absolute;
        width: 27em;
    }
    #demo .yui3-module .yui3-nav ul,
    #demo .yui3-module .yui3-nav li {
        list-style: none;
        margin: 0;
        padding: 0;
    }
    #demo .yui3-module .yui3-nav a {
        color: #ffa928;
    }
    #demo .yui3-module .working {
        background: #fff url(http:&#x2F;&#x2F;l.yimg.com&#x2F;a&#x2F;i&#x2F;nt&#x2F;ic&#x2F;ut&#x2F;bsc&#x2F;busyarr_1.gif) no-repeat 26em 50%;
    }
&lt;&#x2F;style&gt;</pre>


<h3>Example application structure</h3>
<p>For this example, we'll create a simple application that we'll contain under the <code>MyApp</code> namespace.  The basic structure of the namespace will be as follows:</p>

<pre class="code prettyprint">YUI().use(&quot;node&quot;, &quot;transition&quot;, &quot;async-queue&quot;, function (Y) {

var MyApp = {
    &#x2F;&#x2F; the name of the application
    NAME : &quot;AsyncQueue Demo&quot;,

    &#x2F;&#x2F; rendering AsyncQueue
    q : new Y.AsyncQueue(),

    &#x2F;&#x2F; cache of frequently used nodes in the DOM structure
    nodes : {
        root    : null,
        status  : null,
        nav     : null,
        content : null,
        foot    : null
    },

    &#x2F;*** Public API methods ***&#x2F;
    &#x2F;&#x2F; draws the UI in the specified container
    render : function (container) { ... },

    &#x2F;&#x2F; update the status bar at the bottom of the app
    setStatus : function (message,working) { ... },


    &#x2F;*** private methods ***&#x2F;
    &#x2F;&#x2F; adds the basic app skeleton to the page
    _renderFramework : function () { ... },

    &#x2F;&#x2F; populates the navigation section
    _renderNav : function () { ... },

    &#x2F;&#x2F; populates the content section
    _renderContent : function () { ... }
};

});</pre>


<p>The <code>MyApp.render</code> function will add the rendering methods to the <code>MyApp.q</code> AsyncQueue and set it in motion.  Each of the methods will be executed in turn, yielding back to the browser between steps.  So as each piece of the UI is assembled, the browser is given the opportunity to draw it.</p>

<pre class="code prettyprint">...
render : function (container) {
    &#x2F;&#x2F; If the application is currently rendered somewhere, destroy it first
    &#x2F;&#x2F; by clearing the queue and adding the destroy method to run before
    &#x2F;&#x2F; the default rendering operations.
    if (MyApp.nodes.root) {
        MyApp.q.stop();

        MyApp.q.add(
            MyApp.destroy
        );
    }

    &#x2F;&#x2F; Add the rendering operations to the ops.render queue and call run()
    MyApp.q.add(
        &#x2F;&#x2F; pass the container param to the callback using Y.bind
        Y.bind(MyApp._renderFramework, MyApp, container),
        MyApp._renderNav,
        MyApp._renderContent).run();
},
...</pre>


<p>If there are any process-intensive operations in the rendering steps, the UI generated in all <em>previous</em> steps will have been drawn by the browser before the heavy lifting begins.  This way, the user will be shown a part of the UI and can begin to develop an understanding of its structure and operation while the rest of it is being constructed.</p>

<h3>A note on artificial delays and animation</h3>
<p>In this example, rather than include code that would spike your CPU, delays were simulated by inserting AsyncQueue callbacks with a timeout and a function that does nothing.  There is a distinct difference between a delay caused by code execution and a delay caused by <code>setTimeout</code>.  In the former case, the browser is busy and likely won't respond to user events (such as clicks) until the executing code has completed.  In the latter, any number of JavaScript event threads may execute to completion in the intervening time.</p>

<h3>Full Script Source</h3>
<p>The complete code for this example includes the artificial delays added to <code>MyApp.q</code> in the <code>render</code> method.</p>

<pre class="code prettyprint">&lt;script type=&quot;text&#x2F;javascript&quot;&gt;
YUI().use(&quot;node&quot;, &quot;transition&quot;,&quot;async-queue&quot;, function (Y) {

var MyApp = {
    NAME : &#x27;Asynchronous Queue Demo&#x27;,

    q : new Y.AsyncQueue(),

    nodes : {
        root    : null,
        status  : null,
        nav     : null,
        content : null,
        foot    : null
    },

    render : function (container) {
        if (MyApp.nodes.root) {
            MyApp.q.stop();
        }

        &#x2F;&#x2F; artificial delays have been inserted to simulate _renderNav or
        &#x2F;&#x2F; _renderContent being process intensive and taking a while to complete
        MyApp.q.add(
            &#x2F;&#x2F; pass the container param to the callback using Y.bind
            Y.bind(MyApp._renderFramework, MyApp, container),
            {fn: function () {}, timeout: 700}, &#x2F;&#x2F; artificial delay
            MyApp._renderNav,
            {fn: function () {}, timeout: 700}, &#x2F;&#x2F; artificial delay
            MyApp._renderContent).run();
    },

    setStatus : function (message,working) {
        MyApp.nodes.status.setContent(message);

        MyApp.nodes.foot[working?&#x27;addClass&#x27;:&#x27;removeClass&#x27;](&#x27;working&#x27;);
    },

    _renderFramework : function (container) {
        var root = MyApp.nodes.root = Y.one(container);

        root.setContent(
        &#x27;&lt;div class=&quot;yui3-module&quot;&gt;&#x27;+
            &#x27;&lt;div class=&quot;yui3-hd&quot;&gt;&#x27;+
                &#x27;&lt;h4&gt;&#x27;+MyApp.NAME+&#x27;&lt;&#x2F;h4&gt;&#x27;+
            &#x27;&lt;&#x2F;div&gt;&#x27;+
            &#x27;&lt;div class=&quot;yui3-bd&quot;&gt;&#x27;+
                &#x27;&lt;div class=&quot;yui3-nav&quot;&gt;&lt;&#x2F;div&gt;&#x27;+
                &#x27;&lt;div class=&quot;yui3-content&quot;&gt;&lt;&#x2F;div&gt;&#x27;+
            &#x27;&lt;&#x2F;div&gt;&#x27;+
            &#x27;&lt;div class=&quot;yui3-ft&quot;&gt;&#x27;+
                &#x27;&lt;p class=&quot;yui3-status&quot;&gt;&lt;&#x2F;p&gt;&#x27;+
            &#x27;&lt;&#x2F;div&gt;&#x27;+
        &#x27;&lt;&#x2F;div&gt;&#x27;);

        MyApp.nodes.status  = root.one(&#x27;p.yui3-status&#x27;);
        MyApp.nodes.nav     = root.one(&#x27;.yui3-nav&#x27;);
        MyApp.nodes.content = root.one(&#x27;.yui3-content&#x27;);
        MyApp.nodes.foot    = root.one(&#x27;.yui3-ft&#x27;);

        MyApp.nodes.nav.setStyle(&#x27;top&#x27;,&#x27;-30px&#x27;);
        MyApp.nodes.content.setStyle(&#x27;opacity&#x27;,0);

        MyApp.setStatus(&#x27;Loading...&#x27;,true);
    },

    _renderNav : function () {
        var nav = MyApp.nodes.nav;
        nav.append(
            &#x27;&lt;ul class=&quot;yui3-g&quot;&gt;&#x27;+
                &#x27;&lt;li class=&quot;yui3-u-1-4&quot;&gt;&lt;a href=&quot;#&quot;&gt;Nav Lorem&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;&#x27;+
                &#x27;&lt;li class=&quot;yui3-u-1-4&quot;&gt;&lt;a href=&quot;#&quot;&gt;Nav Ipsum&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;&#x27;+
                &#x27;&lt;li class=&quot;yui3-u-1-4&quot;&gt;&lt;a href=&quot;#&quot;&gt;Nav Dolor&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;&#x27;+
                &#x27;&lt;li class=&quot;yui3-u-1-4&quot;&gt;&lt;a href=&quot;#&quot;&gt;Nav Sit&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;&#x27;+
            &#x27;&lt;&#x2F;ul&gt;&#x27;);

        nav.transition({
            top: 0,
            duration: .3
        });

        &#x2F;&#x2F; Stub some navigation behavior for the example
        nav.delegate(&#x27;click&#x27;, function (e) {
            e.preventDefault();
            MyApp.nodes.content
                .setContent(&#x27;&lt;p&gt;Clicked on &#x27; + this.get(&#x27;text&#x27;) + &#x27;&lt;&#x2F;p&gt;&#x27;);
        }, &#x27;a&#x27;);
    },

    _renderContent : function () {
        MyApp.nodes.content
            .setContent(&#x27;&lt;p&gt;[ App content here ]&lt;&#x2F;p&gt;&#x27;)
            .transition({
                opacity: 1,
                duration: .8
            });

        MyApp.setStatus(&#x27;App initialized&#x27;,false);
    }
};

Y.one(&#x27;#init&#x27;).on(&#x27;click&#x27;,function (e) {
    e.preventDefault();
    this.set(&#x27;text&#x27;,&#x27;Re-initialize Application&#x27;);

    MyApp.render(&#x27;#demo&#x27;);
});

&#x2F;&#x2F; expose the example structure
YUI.example = { MyApp : MyApp };

});
&lt;&#x2F;script&gt;</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="This example employs AsyncQueue to incrementally construct an application interface; this illustrates the approach you&#x27;d take to allow chunked rendering of the UI in a process-intensive application.">
                                        <a href="queue-app.html">Building a UI with AsyncQueue</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
